package com.centling.smartSealForPhone.activity

import android.graphics.Color
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.Editable
import android.text.InputFilter
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.centling.smartSealForPhone.utils.TextLengthWatch
import com.jzxiang.pickerview.TimePickerDialog
import com.jzxiang.pickerview.data.Type
import kotlinx.android.synthetic.main.activity_apply_boss_mode.*
import java.text.SimpleDateFormat
import java.util.*

class ApplyBossModeActivity : TitleBarActivity(), View.OnClickListener {
    private var stampTime = 0L
    private var  exitTime:Long=0L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_apply_boss_mode)
        setTitleBarText("解锁模式申请")
        initView()
    }

    private fun initView() {
        boss_mode_apply_date.setOnClickListener(this)
        boss_mode_apply_sure.setOnClickListener(this)
        boss_mode_apply_reason.filters = arrayOf<InputFilter>(android.text.InputFilter.LengthFilter(100))//限制字数

        boss_mode_apply_reason.addTextChangedListener(TextLengthWatch(100))//字数提醒

        boss_mode_apply_reason.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable) {

                tv_apply_boss_words_limit.text = String.format(getString(R.string.number_limit_100, s.toString().length))
                if (s.toString().length == 100) {
                    tv_apply_boss_words_limit.setTextColor(Color.RED)
                } else {
                    tv_apply_boss_words_limit.setTextColor(Color.BLACK)
                }

            }
        })
    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.boss_mode_apply_date -> showDataPickerDialogStartTime()
            R.id.boss_mode_apply_sure -> {
                if(System.currentTimeMillis()- exitTime >500)
                {
                val applyName = boss_mode_apply_name.text.toString()
                val reason = boss_mode_apply_reason.text.toString()
                if (TextUtils.isEmpty(applyName)) {
                    ShowToast.shortTime("请填写申请人")
                } else if (stampTime == 0L) {
                    ShowToast.shortTime("请选择日期")
                } else if (TextUtils.isEmpty(reason)) {
                    ShowToast.shortTime("请填写申请原因")
                } else {
                    val param = HashMap<String, String>()
                    param.put("applyReason", reason)
                    param.put("appllicant", applyName)
                    param.put("startTime", stampTime.toString())
                    postJsonEnqueue(UrlConstants.APPLY_BOSS_MODE, param, object : OkHttpUtil.NetCallBack {
                        override fun onSucceed(json: String) {
                            ShowToast.shortTime("申请提交成功")
                            finish()
                        }

                            override fun onFailed(reason: String) {
                                ShowToast.shortTime(reason)
                            }
                        })
                        exitTime = System.currentTimeMillis()
                    }
                }
            }
        }
    }

    private fun showDataPickerDialogStartTime() {
        TimePickerDialog.Builder().setCallBack { _, millseconds ->
            val time = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(
                    Date(millseconds))
            boss_mode_apply_date.text = time;
            stampTime = millseconds;
        }.setCancelStringId("取消").setSureStringId("确定").setTitleStringId("请选择开始时间").setYearText(
                "年").setMonthText("月").setDayText("日").setCyclic(false).setMinMillseconds(System.currentTimeMillis())
                .setCurrentMillseconds(System.currentTimeMillis()).setThemeColor(ContextCompat.getColor(mContext, R.color.blue1))
                .setType(Type.YEAR_MONTH_DAY).setWheelItemTextNormalColor(ContextCompat.getColor(mContext, R.color.grey1))
                .setWheelItemTextSelectorColor(ContextCompat.getColor(this, R.color.blue1))
                .setWheelItemTextSize(18).build().show(supportFragmentManager, "year_month_day")
    }
}




